/**
 * 
 */
package com.celephais.common.client.ui;

import java.util.List;

import com.celephais.common.client.model.ReferenceUI;
import com.celephais.common.client.model.TransferObject;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * @author Eugene Shen
 *
 */
public class ReferencePickerDialog extends DialogBox {
	
	private ModalDialogCallback owner;
	private String refColName;
	private List<? extends TransferObject> validRefs;
	private ReferenceUI currentPick;
	private VerticalPanel outerPanel;
	private ListBox listBox;
	private HorizontalPanel btnsPanel;
	private Button okayBtn;
	private Button cancelBtn;
	
	public ReferencePickerDialog(ModalDialogCallback owner) {
		super(false, true);
		this.owner = owner;
		initComps();
	}
	
	public ReferencePickerDialog(ModalDialogCallback owner, String refColName,
			List<? extends TransferObject> validRefs, ReferenceUI currentPick) {
		this(owner);
		setRefColName(refColName);
		setValidRefs(validRefs);
		setCurrentPick(currentPick);
	}
	
	private void initComps() {
		outerPanel = new VerticalPanel();
		listBox = new ListBox();
		btnsPanel = new HorizontalPanel();
		okayBtn = new Button("OK");
		cancelBtn = new Button("Cancel");
		
		outerPanel.add(listBox);
		outerPanel.add(btnsPanel);
		btnsPanel.add(okayBtn);
		btnsPanel.add(cancelBtn);
		
		okayBtn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				int selected = listBox.getSelectedIndex();
				currentPick = ReferenceUI.createReference(validRefs.get(selected));
				ReferencePickerDialog.this.hide();
				if (owner != null) {
					owner.onDialogConfirm(ReferencePickerDialog.this);
				}
			}
		});
		
		cancelBtn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ReferencePickerDialog.this.hide();
				if (owner != null) {
					owner.onDialogCancel(ReferencePickerDialog.this);
				}
			}
		});
		
		this.add(outerPanel);
	}
	
	public void center(String refColName, List<? extends TransferObject> validRefs, ReferenceUI currentPick) {
		setRefColName(refColName);
		setValidRefs(validRefs);
		setCurrentPick(currentPick);
		center();
	}
	
	public void setRefColName(String name) {
		this.refColName = name;
	}
	public String getRefColName() {
		return refColName;
	}
	
	public void setValidRefs(List<? extends TransferObject> validRefs) {
		this.validRefs = validRefs;
		
		listBox.clear();
		for (TransferObject obj : validRefs) {
			listBox.addItem(obj.getName() + " : " + obj.getKey());
		}
	}
	
	public void setCurrentPick(ReferenceUI currentPick) {
		this.currentPick = currentPick;
		
		if (currentPick != null) {
			for (int i=0; i<validRefs.size(); ++i) {
				if (currentPick.getKey().equals(validRefs.get(i).getKey())) {
					listBox.setSelectedIndex(i);
					break;
				}
			}
		}
	}
	public ReferenceUI getCurrentPick() {
		return currentPick;
	}
}
